package com.miaomiaojia.control;

import com.miaomiaojia.android.BrowserActivity;
import com.miaomiaojia.android.R;
import com.miaomiaojia.entity.Price;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class BtnPrice extends TableLayout {
	TextView titleTextView;
	TextView priceTextView;
	ImageView icoImageView;
	LinearLayout icoLayout;
	Price price;
	TableRow tablerow;
	Context context;

	public void setPrice(Price price) {
		this.price = price;
		this.titleTextView.setText(price.getTitle());
		this.priceTextView.setText("￥" + String.valueOf(price.getPrice()));
	}

	public BtnPrice(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public BtnPrice(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	private void init() {
		this.tablerow = new TableRow(this.context);
		this.titleTextView = new TextView(this.context);
		this.priceTextView = new TextView(this.context);
		this.icoImageView = new ImageView(this.context);
		this.icoLayout = new LinearLayout(this.context);
		this.icoLayout.addView(icoImageView);
		this.tablerow.addView(this.titleTextView);
		this.tablerow.addView(this.priceTextView);
		this.tablerow.addView(this.icoLayout);
		this.tablerow.setGravity(Gravity.CENTER_VERTICAL);

		// this.tablerow.setBackgroundDrawable(
		// this.getResources().getDrawable(R.drawable.rightgo));
		this.addView(this.tablerow);

		// this.setLayoutParams(new LayoutParams(
		// LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		// this.tablerow.setBackgroundDrawable(this.getResources().getDrawable(
		// R.drawable.pricetext));
		this.setColumnStretchable(0, true);

		this.priceTextView.setTextSize(9);
		this.priceTextView.setBackgroundDrawable(this.getResources()
				.getDrawable(R.drawable.pricetext));

		LayoutParams lp = new LayoutParams(23, 23);
		lp.setMargins(10, 0, 0, 0);
		this.icoImageView.setLayoutParams(lp);

		this.icoImageView.setImageResource(R.drawable.rightgo);

		// LayoutParams lp=new LayoutParams();
		// lp.setMargins(10, 0, 0, 0);
		// this.icoImageView.setLayoutParams(lp);
		// this.icoImageView.setScaleType(ScaleType.FIT_XY);
		// this.icoImageView.setMaxWidth(23);
		// this.icoImageView.setMaxHeight(23);
		// this.icoImageView.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.pricebutton));
		// this.icoImageView.setMaxWidth(23);
		// this.icoImageView.setMaxHeight(23);
		//
		// this.icoLayout.setMinimumWidth((int) ((width - px) * 0.1));
		// this.icoLayout.setGravity(Gravity.RIGHT);
		// this.icoLayout.setPadding(0, 0, 2, 0);
		// this.icoLayout.setBackgroundColor(Color.GRAY);

		this.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent urlIntent = new Intent(context, BrowserActivity.class);
				Bundle urlBundle = new Bundle();
				urlBundle.putString("url", price.getUrl());
				urlIntent.putExtras(urlBundle);
				context.startActivity(urlIntent);
			}
		});
	}
	// @Override
	// protected void onDraw(Canvas canvas) {
	//
	// super.onDraw(canvas);
	// Rect rec = canvas.getClipBounds();
	// Paint paint = new Paint();
	// paint.setColor(Color.RED);
	// int a=canvas.getHeight();
	// canvas.drawText("画圆：", rec.right-100, rec.centerY()+5, paint);
	// }
}
